/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core.action.retrieve;

import com.inspur.edp.bff.api.manager.context.RetrieveContext;
import com.inspur.edp.bff.spi.action.retrieve.AbstractRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.AfterRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.BeforeRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.DefaultRetrieveAction;
import com.inspur.edp.bff.spi.assembler.AbstractExtendRetrieveAssembler;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.collection.VMActionCollection;
import com.inspur.edp.formserver.viewmodel.common.InternalExtendActionUtil;

import java.util.ArrayList;

import static com.inspur.edp.bff.engine.core.action.BffEngineAssemblerBase.getActions;

public class BffEngineRetrieveAssembler extends AbstractExtendRetrieveAssembler {

    private GspViewModel vo;

    public BffEngineRetrieveAssembler(GspViewModel vo) {
        this.vo = vo;
    }

    @Override
    protected ArrayList<BeforeRetrieveAction> getBeforeRetrieveActions(RetrieveContext retrieveContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getBeforeRetrieveActions();
        return getActions(BeforeRetrieveAction.class, actions, getParamTypes(), getParamValues(retrieveContext), null, null);
    }

    @Override
    protected ArrayList<AbstractRetrieveAction> getRetrieveActions(RetrieveContext retrieveContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getRetrieveActions();
        return getActions(
                AbstractRetrieveAction.class,
                actions,
                getParamTypes(),
                getParamValues(retrieveContext),
                new DefaultRetrieveAction(retrieveContext),
                InternalExtendActionUtil.RetrieveActionId);
    }

    @Override
    protected ArrayList<AfterRetrieveAction> getAfterRetrieveActions(RetrieveContext retrieveContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getAfterRetrieveActions();
        return getActions(AfterRetrieveAction.class, actions, getParamTypes(), getParamValues(retrieveContext), null, null);
    }

    private ArrayList<Class> getParamTypes() {
        ArrayList<Class> paramTypes = new ArrayList<>();
        paramTypes.add(RetrieveContext.class);
        return paramTypes;
    }

    private ArrayList<Object> getParamValues(RetrieveContext retrieveContext) {
        ArrayList<Object> values = new ArrayList<>();
        values.add(retrieveContext);
        return values;
    }
}
